﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace AIC_CD
{
    public partial class BaoCaoBanChapHanhUBKTCDCS_Update : MyFromBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lblNotice.Visible = false;

            CheckPermission();
            if (!IsPostBack)
            {
                Config();
                BindDataToControls();
            }
        }

        /// <summary>
        /// Thủ tục kiểm tra quyền sử dụng của người dùng có được quyền truy cập vào mục này hay không?
        /// </summary>
        private void CheckPermission()
        {
            Dictionary<string, int> Role = MyCommon.CheckPermission("DanhSachBCHUBKT");
            if (Role == null || Role["Xem"] == 0 || ((Role["Xem"] == 1 && Role["Sua"] == 0)))
            {
                Response.Redirect("Index.aspx?p=0");
            }
        }

        /// <summary>
        /// Thủ tục cấu hình giao diện, các controls
        /// </summary>
        private void Config()
        {
            //TODO
            txtThuTruongCoQuanChinhTri.Text = "";
        }

        /// <summary>
        /// Bind Data vào các contrls như Combobox, ListBox, textBox ...
        /// </summary>
        private void BindDataToControls()
        {
            string sql = "Select ID,TenDM From tblDanhMuc Where MaDM='" + MyConst.DanhMuc.KyBaoCao.Ma + "' ";
            SqlDataReader reKyBaoCao = SqlHelper.ExecuteReader(CommandType.Text, sql);
            clsDataBind.CreateCombo("Tất cả", reKyBaoCao, cboKyBaoCao, 1, 0);
        }

        /// <summary>
        /// Bind Data vào gridview
        /// </summary>
        private void BindDataToGrid()
        {
            bool IsEmpty = false;
            List<SqlParameter> arrParams = new List<SqlParameter>();
            arrParams.Add(SqlHelper.CreateParameterObject("@KyBaoCaoID_DM", SqlDbType.Int, ParameterDirection.Input, cboKyBaoCao.SelectedValue));
            arrParams.Add(SqlHelper.CreateParameterObject("@DonViID", SqlDbType.Int, ParameterDirection.Input, MyCommon.GetDonViID(Session["userid"].ToString())));

            string sql = "SELECT * FROM dbo.tblDanhSachBCH_UBKTCDCS WHERE DonViID = @DonViID AND KyBaoCaoID_DM = @KyBaoCaoID_DM AND IsCommon = 0";
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, sql, arrParams);
            if (dt.Rows.Count == 0)
            {
                IsEmpty = true;
                DataRow dr = dt.NewRow();
                dr["HoTen"] = "";
                dr["QueQuan"] = "";
                dr["NamSinhNam"] = 0;
                dr["NamSinhNu"] = 0;
                dr["ChucVuChinhTri"] = "";
                dr["ChucVuChuyenTrach"] = "";
                dr["DangVien"] = "";
                dr["DanToc"] = "";
                dr["TrinhDoHocVan"] = "";
                dr["TrinhDoChinhTri"] = "";
                dr["TrinhDoChuyenMon"] = "";
                dt.Rows.InsertAt(dr, 0);

            }
            grvData.ShowFooter = true;
            grvData.DataSource = dt;
            grvData.DataBind();
            if (IsEmpty)
            {
                grvData.Rows[0].Visible = false;
            }
            BindDataToTextField();
        }

        protected void BindDataToTextField()
        {
            string sql = "SELECT * FROM dbo.tblDanhSachBCH_UBKTCDCS WHERE KyBaoCaoID_DM = @KyBaoCaoID_DM AND DonViID = @DonViID AND IsCommon = 1";
            List<SqlParameter> lstParameters = new List<SqlParameter>();
            lstParameters.Add(SqlHelper.CreateParameterObject("@KyBaoCaoID_DM", SqlDbType.Int, ParameterDirection.Input, cboKyBaoCao.SelectedValue));
            lstParameters.Add(SqlHelper.CreateParameterObject("@DonViID", SqlDbType.Int, ParameterDirection.Input, MyCommon.GetDonViID(Session["userid"].ToString())));
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, sql, lstParameters);
            if (dt.Rows.Count != 0)
            {
                txtTenBaoCao.Text = dt.Rows[0]["CM_TenBaoCao"].ToString();
                txtThuTruongCoQuanChinhTri.Text = dt.Rows[0]["CM_ThuTruong"].ToString();
            }
            else
            {
                txtTenBaoCao.Text = "";
                txtThuTruongCoQuanChinhTri.Text = "";
            }
        }

        #region Events

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("Index.aspx");
        }

        protected void cboKyBaoCao_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboKyBaoCao.SelectedIndex > 0)
            {
                btnLuu.Visible = true;
                grvData.Visible = true;
                grvData.EditIndex = -1;
                BindDataToGrid();
            }
            else
            {
                btnLuu.Visible = false;
                grvData.Visible = false;
            }
        }

        protected void btnLuu_Click(object sender, EventArgs e)
        {
            String sql = "SELECT * FROM dbo.tblDanhSachBCH_UBKTCDCS WHERE KyBaoCaoID_DM = @KyBaoCaoID_DM AND DonViID = @DonViID AND IsCommon = 1";
            List<SqlParameter> lstParameters = new List<SqlParameter>();
            lstParameters.Add(SqlHelper.CreateParameterObject("@KyBaoCaoID_DM", SqlDbType.Int, ParameterDirection.Input, cboKyBaoCao.SelectedValue));
            lstParameters.Add(SqlHelper.CreateParameterObject("@DonViID", SqlDbType.Int, ParameterDirection.Input, MyCommon.GetDonViID(Session["userid"].ToString())));

            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, sql, lstParameters);
            if (dt.Rows.Count == 0)
            {
                btnLuu.CommandName = MyConst.Action.Addnew;
            }
            else
            {
                btnLuu.CommandName = MyConst.Action.Update;
            }

            if (btnLuu.CommandName == MyConst.Action.Addnew)
            {
                string sSQL = "INSERT INTO dbo.tblDanhSachBCH_UBKTCDCS" +
                              "(KyBaoCaoID_DM ,DonViID ,CM_ThuTruong ,CM_TenBaoCao ,CM_ThoiGianBaoCao" +
                              ",IsCommon ,CreateDate ,UpdateDate ,UserID)" +
                              "VALUES  ( @KyBaoCaoID_DM ,@DonViID ,@CM_ThuTruong ,@CM_TenBaoCao ," +
                              "GETDATE() ,1 ,GETDATE() ,GETDATE() ,@UserID)";

                List<SqlParameter> lstParameter = new List<SqlParameter>();
                lstParameter.Add(SqlHelper.CreateParameterObject("@KyBaoCaoID_DM", SqlDbType.Int, ParameterDirection.Input, cboKyBaoCao.SelectedValue));
                lstParameter.Add(SqlHelper.CreateParameterObject("@DonViID", SqlDbType.Int, ParameterDirection.Input, MyCommon.GetDonViID(Session["userid"].ToString())));
                lstParameter.Add(SqlHelper.CreateParameterObject("@CM_ThuTruong", SqlDbType.NVarChar, ParameterDirection.Input, txtThuTruongCoQuanChinhTri.Text.Trim()));
                lstParameter.Add(SqlHelper.CreateParameterObject("@CM_TenBaoCao", SqlDbType.NVarChar, ParameterDirection.Input, txtTenBaoCao.Text.Trim().ToUpper()));
                lstParameter.Add(SqlHelper.CreateParameterObject("@UserID", SqlDbType.Int, ParameterDirection.Input, Convert.ToInt32(Session["userid"])));

                SqlHelper.ExecuteNonQuery(CommandType.Text, sSQL, lstParameter);
                MyCommon.AddMessage(this.Master, "Lưu thông tin báo cáo thành công", MyConst.MessageType.Success);
            }
            else
            {
                string sSQL = "Update tblDanhSachBCH_UBKTCDCS Set CM_ThuTruong=@CM_ThuTruong, CM_TenBaoCao=@CM_TenBaoCao " +
                                "Where IsCommon=1 And DonViID=" + MyCommon.GetDonViID(Session["userid"].ToString()) + " And KyBaoCaoID_DM=" + cboKyBaoCao.SelectedValue;

                List<SqlParameter> lstParameter = new List<SqlParameter>();
                lstParameter.Add(SqlHelper.CreateParameterObject("@CM_ThuTruong", SqlDbType.NVarChar, ParameterDirection.Input, txtThuTruongCoQuanChinhTri.Text.Trim()));
                lstParameter.Add(SqlHelper.CreateParameterObject("@CM_TenBaoCao", SqlDbType.NVarChar, ParameterDirection.Input, txtTenBaoCao.Text.Trim().ToUpper()));


                SqlHelper.ExecuteNonQuery(CommandType.Text, sSQL, lstParameter);

                MyCommon.AddMessage(this.Master, "Lưu thông tin báo cáo thành công", MyConst.MessageType.Success);
            }
        }

        #endregion Events


        #region GridCommand

        private static void AddMergedCells(GridViewRow objgridviewrow, TableCell objtablecell, int colspan, int rowspan, string celltext)
        {
            objtablecell = new TableCell();
            objtablecell.Text = celltext;
            objtablecell.ColumnSpan = colspan;
            objtablecell.RowSpan = rowspan;
            objtablecell.HorizontalAlign = HorizontalAlign.Center;
            objgridviewrow.Cells.Add(objtablecell);
        }

        protected void grvData_RowCreated(object sender, GridViewRowEventArgs e)
        {

            if (e.Row.RowType == DataControlRowType.Header)
            {

                GridView objGridView = (GridView)sender;
                //GridViewRow objgridviewrow = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);

                GridViewRow objgridviewrow = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);
                GridViewRow objgridviewrow2 = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);
                GridViewRow objgridviewrow3 = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);
                TableCell objtablecell = new TableCell();
                AddMergedCells(objgridviewrow, objtablecell, 1, 2, "STT");


                AddMergedCells(objgridviewrow, objtablecell, 1, 2, "Họ và tên");

                AddMergedCells(objgridviewrow, objtablecell, 1, 2, "Quê quán");
                AddMergedCells(objgridviewrow, objtablecell, 2, 1, "Năm sinh");
                AddMergedCells(objgridviewrow, objtablecell, 2, 1, "Chức vụ hiện nay");
                AddMergedCells(objgridviewrow, objtablecell, 1, 2, "Đảng viên");
                AddMergedCells(objgridviewrow, objtablecell, 1, 2, "Dân tộc");
                AddMergedCells(objgridviewrow, objtablecell, 3, 1, "Trình độ");
                AddMergedCells(objgridviewrow, objtablecell, 1, 2, "Sửa");
                objgridviewrow.BackColor = System.Drawing.Color.FromArgb(0x10, 0x9A, 0xEA);

                objGridView.Controls[0].Controls.AddAt(0, objgridviewrow);

                objtablecell = new TableCell();
                AddMergedCells(objgridviewrow2, objtablecell, 1, 1, "Nam");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 1, "Nữ");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 1, "Công đoàn");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 1, "Chuyên môn");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 1, "Học vấn");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 1, "Chính trị");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 1, "Chuyên môn");

                objgridviewrow2.BackColor = System.Drawing.Color.FromArgb(0x10, 0x9A, 0xEA);
                objGridView.Controls[0].Controls.AddAt(1, objgridviewrow2);
            }
        }

        protected void grvData_RowDataBound(object sender, GridViewRowEventArgs e)
        {


            RowDataBound(sender, e);
        }

        protected void grvData_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string sID = e.CommandArgument.ToString().Trim();
            string sCommandName = e.CommandName;


            if ((sCommandName == AIC_CD.MyConst.Action.Approve))
            {
                SqlTransaction MyTrans = SqlHelper.BeginTransaction();
                try
                {

                    // cap nhat vao database
                    TextBox txtHoTenEdit = (TextBox)grvData.Rows[grvData.EditIndex].FindControl("txtHoTenEdit");
                    TextBox txtQueQuanEdit = (TextBox)grvData.Rows[grvData.EditIndex].FindControl("txtQueQuanEdit");
                    TextBox txtNamSinhNamEdit = (TextBox)grvData.Rows[grvData.EditIndex].FindControl("txtNamSinhNamEdit");
                    TextBox txtNamSinhNuEdit = (TextBox)grvData.Rows[grvData.EditIndex].FindControl("txtNamSinhNuEdit");
                    TextBox txtChucVuCTEdit = (TextBox)grvData.Rows[grvData.EditIndex].FindControl("txtChucVuCTEdit");
                    TextBox txtChucVuCTrachEdit = (TextBox)grvData.Rows[grvData.EditIndex].FindControl("txtChucVuCTrachEdit");
                    TextBox txtDangVienEdit = (TextBox)grvData.Rows[grvData.EditIndex].FindControl("txtDangVienEdit");
                    TextBox txtDanTocEdit = (TextBox)grvData.Rows[grvData.EditIndex].FindControl("txtDanTocEdit");
                    TextBox txtHocVanEdit = (TextBox)grvData.Rows[grvData.EditIndex].FindControl("txtHocVanEdit");
                    TextBox txtCTriEdit = (TextBox)grvData.Rows[grvData.EditIndex].FindControl("txtCTriEdit");
                    TextBox txtChuyenMonEdit = (TextBox)grvData.Rows[grvData.EditIndex].FindControl("txtChuyenMonEdit");

                    string sSQL = "UPDATE tblDanhSachBCH_UBKTCDCS " +
                                  "SET HoTen = @HoTen,QueQuan = @QueQuan,NamSinhNam = @NamSinhNam,NamSinhNu = @NamSinhNu,ChucVuChinhTri = @ChucVuChinhTri" +
                                  ",ChucVuChuyenTrach = @ChucVuChuyenTrach,DangVien = @DangVien,DanToc = @DanToc" +
                                  ",TrinhDoHocVan = @TrinhDoHocVan,TrinhDoChinhTri = @TrinhDoChinhTri,TrinhDoChuyenMon = @TrinhDoChuyenMon" +
                                  ",UpdateDate = GETDATE(),UserID = @UserID WHERE ID = @ID";

                    List<SqlParameter> lstParameter = new List<SqlParameter>();

                    lstParameter.Add(SqlHelper.CreateParameterObject("@HoTen", SqlDbType.NVarChar, ParameterDirection.Input, txtHoTenEdit.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@QueQuan", SqlDbType.NVarChar, ParameterDirection.Input, txtQueQuanEdit.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@NamSinhNam", SqlDbType.Int, ParameterDirection.Input, txtNamSinhNamEdit.Text.Trim().Equals("") ? "0" : txtNamSinhNamEdit.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@NamSinhNu", SqlDbType.Int, ParameterDirection.Input, txtNamSinhNuEdit.Text.Trim().Equals("") ? "0" : txtNamSinhNuEdit.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@ChucVuChinhTri", SqlDbType.NVarChar, ParameterDirection.Input, txtChucVuCTEdit.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@ChucVuChuyenTrach", SqlDbType.NVarChar, ParameterDirection.Input, txtChucVuCTrachEdit.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@DangVien", SqlDbType.NVarChar, ParameterDirection.Input, txtDangVienEdit.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@DanToc", SqlDbType.NVarChar, ParameterDirection.Input, txtDanTocEdit.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@TrinhDoHocVan", SqlDbType.NVarChar, ParameterDirection.Input, txtHocVanEdit.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@TrinhDoChinhTri", SqlDbType.NVarChar, ParameterDirection.Input, txtCTriEdit.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@TrinhDoChuyenMon", SqlDbType.NVarChar, ParameterDirection.Input, txtChuyenMonEdit.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@UserID", SqlDbType.Int, ParameterDirection.Input, Session["userid"]));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@ID", SqlDbType.Int, ParameterDirection.Input, sID));
                    
                    SqlHelper.ExecuteNonQuery(CommandType.Text, sSQL, lstParameter);
                    MyTrans.Commit();
                    grvData.EditIndex = -1;
                    BindDataToGrid();
                    MyCommon.AddMessage(this.Master, "Cập nhật thành công", MyConst.MessageType.Success);
                }
                catch (Exception)
                {
                    MyTrans.Rollback();
                }

            }
            else if (e.CommandName == AIC_CD.MyConst.Action.Addnew)
            {
                SqlTransaction MyTrans = SqlHelper.BeginTransaction();
                try
                {
                    TextBox txtHoTenNew = (TextBox)grvData.FooterRow.FindControl("txtHoTenNew");
                    TextBox txtQueQuanNew = (TextBox)grvData.FooterRow.FindControl("txtQueQuanNew");
                    TextBox txtNamSinhNamNew = (TextBox)grvData.FooterRow.FindControl("txtNamSinhNamNew");
                    TextBox txtNamSinhNuNew = (TextBox)grvData.FooterRow.FindControl("txtNamSinhNuNew");
                    TextBox txtChucVuCTNew = (TextBox)grvData.FooterRow.FindControl("txtChucVuCTNew");
                    TextBox txtChucVuCTrachNew = (TextBox)grvData.FooterRow.FindControl("txtChucVuCTrachNew");
                    TextBox txtDangVienNew = (TextBox)grvData.FooterRow.FindControl("txtDangVienNew");
                    TextBox txtDanTocNew = (TextBox)grvData.FooterRow.FindControl("txtDanTocNew");
                    TextBox txtHocVanNew = (TextBox)grvData.FooterRow.FindControl("txtHocVanNew");
                    TextBox txtCTriNew = (TextBox)grvData.FooterRow.FindControl("txtCTriNew");
                    TextBox txtChuyenMonNew = (TextBox)grvData.FooterRow.FindControl("txtChuyenMonNew");

                    string sql = "INSERT INTO dbo.tblDanhSachBCH_UBKTCDCS " +
                                 "( KyBaoCaoID_DM ,DonViID ,HoTen ,QueQuan ,NamSinhNam ,NamSinhNu ,ChucVuChinhTri ,ChucVuChuyenTrach ,DangVien ,DanToc ," +
                                 "TrinhDoHocVan ,TrinhDoChinhTri ,TrinhDoChuyenMon ,CreateDate ,UpdateDate ,UserID, IsCommon)" +
                                 "VALUES  ( @KyBaoCaoID_DM ,@DonViID ,@HoTen ,@QueQuan ,@NamSinhNam ,@NamSinhNu ,@ChucVuChinhTri ,@ChucVuChuyenTrach ,@DangVien ,@DanToc ," +
                                 "@TrinhDoHocVan ,@TrinhDoChinhTri ,@TrinhDoChuyenMon ,GETDATE() ,GETDATE() ,@UserID, 0)";
                    List<SqlParameter> lstParameter = new List<SqlParameter>();
                    lstParameter.Add(SqlHelper.CreateParameterObject("@KyBaoCaoID_DM", SqlDbType.Int, ParameterDirection.Input, Convert.ToInt32(cboKyBaoCao.SelectedValue)));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@DonViID", SqlDbType.Int, ParameterDirection.Input, MyCommon.GetDonViID(Session["userid"].ToString())));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@HoTen", SqlDbType.NVarChar, ParameterDirection.Input, txtHoTenNew.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@QueQuan", SqlDbType.NVarChar, ParameterDirection.Input, txtQueQuanNew.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@NamSinhNam", SqlDbType.Int, ParameterDirection.Input, txtNamSinhNamNew.Text.Trim().Equals("") ? "0" : txtNamSinhNamNew.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@NamSinhNu", SqlDbType.Int, ParameterDirection.Input, txtNamSinhNuNew.Text.Trim().Equals("") ? "0" : txtNamSinhNuNew.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@ChucVuChinhTri", SqlDbType.NVarChar, ParameterDirection.Input, txtChucVuCTNew.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@ChucVuChuyenTrach", SqlDbType.NVarChar, ParameterDirection.Input, txtChucVuCTrachNew.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@DangVien", SqlDbType.NVarChar, ParameterDirection.Input, txtDangVienNew.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@DanToc", SqlDbType.NVarChar, ParameterDirection.Input, txtDanTocNew.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@TrinhDoHocVan", SqlDbType.NVarChar, ParameterDirection.Input, txtHocVanNew.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@TrinhDoChinhTri", SqlDbType.NVarChar, ParameterDirection.Input, txtCTriNew.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@TrinhDoChuyenMon", SqlDbType.NVarChar, ParameterDirection.Input, txtChuyenMonNew.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@UserID", SqlDbType.Int, ParameterDirection.Input, Convert.ToInt32(Session["userid"].ToString())));

                    int Result = SqlHelper.ExecuteNonQuery(CommandType.Text, sql, lstParameter);
                    if (Result == 0)
                    {
                        MyCommon.AddMessage(this.Master, "Có lỗi xảy ra trong quá trình thêm mới", MyConst.MessageType.Error);
                    }
                    else
                    {
                        MyCommon.AddMessage(this.Master, "Thêm mới thành công !", MyConst.MessageType.Success);
                    }

                    MyTrans.Commit();
                    BindDataToGrid();
                }
                catch (Exception)
                {
                    MyCommon.AddMessage(this.Master, "Có lỗi xảy ra trong quá trình thêm mới", MyConst.MessageType.Error);
                    MyTrans.Rollback();
                }
            }
            else if ((sCommandName == AIC_CD.MyConst.Action.Delete))
            {
                string sql = "DELETE FROM dbo.tblDanhSachBCH_UBKTCDCS WHERE ID = @ID";
                List<SqlParameter> lstParameter = new List<SqlParameter>();

                lstParameter.Add(SqlHelper.CreateParameterObject("@ID", SqlDbType.Int, ParameterDirection.Input, sID));
                SqlHelper.ExecuteNonQuery(CommandType.Text, sql, lstParameter);

                grvData.EditIndex = -1;
                BindDataToGrid();
                MyCommon.AddMessage(this.Master, "Xóa thành công !", MyConst.MessageType.Success);
            }
        }

        protected void grvData_RowEditing(object sender, GridViewEditEventArgs e)
        {
            grvData.EditIndex = e.NewEditIndex;
            BindDataToGrid();
        }



        protected void grvData_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            //grvData.PageIndex = e.NewPageIndex;
            //BindDataToGrid();
        }


        protected void grvData_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            grvData.EditIndex = -1;
            BindDataToGrid();
        }

        #endregion GridCommand

        protected void grvData_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            grvData.DeleteRow(e.RowIndex);
            grvData.DataBind();
        }
    }
}
